import RankingItem from "./scripts/RankingItem";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class ScollHelper extends cc.Component {

    @property(cc.ScrollView)
    scollView: cc.ScrollView = null;


    @property(cc.Node)
    nodeItem: cc.Node = null;
    

    @property(cc.Node)
    nodeContent: cc.Node = null;
    
    //初始item 的数量
    initCount:number = 0;
    //item 的最大数量
    maxCount:number = 100;

    //当前索引
    curIndex:number = 0;
    //偏移索引
    offsetIndex:number = 0;
    //上次的y的位置
    lastPosY:number = 0;

    refreshItemCallBack:Function;


    start () { //初始显示的数量
        console.log(this.nodeItem)
        console.log(this.nodeContent)
      //  this.initItems(10,null);

    }

    initItems(count:number,callBack:Function){
        this.refreshItemCallBack = callBack;
        if(this.nodeContent.childrenCount == 0){
            for (let index = 0; index < 8; index++) { // 60 
                let itemNode =  cc.instantiate(this.nodeItem)
                this.nodeContent.addChild(itemNode);
                itemNode.position = new cc.Vec2(0,index*-88 - 5 - 44);
            }
        }
        //隐藏所有
        for (let index = 0; index < 8; index++) { // 60 
            this.nodeContent.children[index].position = new cc.Vec2(0,index*-88 - 5 - 44);
           this.nodeContent.children[index].active = false
        }
        this.maxCount = count
        if(count > 8){
            count = 8
        }

        for (let index = 0; index < count; index++) { // 60 
            let itemNode:cc.Node = this.nodeContent.children[index];
            this.initCount = index;
            itemNode.active = true;
            this.refreshItem(index,0,itemNode);
        }
        this.nodeContent.height = this.maxCount * 88;
        this.lastPosY = this.nodeContent.position.y
        this.offsetIndex = 0;

        this.nodeContent.position = new cc.Vec2(0,308)
    }

    update (dt) {

        let isUp = false;
        if(this.lastPosY < this.nodeContent.position.y){ //上面
            isUp = true;
        }
        if(this.nodeContent.position.y < 308){
            this.scollView.stopAutoScroll();
            this.nodeContent.position = new cc.Vec2(0,308)
        }else if(this.nodeContent.position.y >= 308 + this.maxCount*88){
            this.scollView.stopAutoScroll();
            this.nodeContent.position = new cc.Vec2(0,308 + this.maxCount*88)
        }
        
        let index = Math.floor((this.nodeContent.position.y - 308)/88)
        this.lastPosY = this.nodeContent.position.y;

        if(index < 0 || index  + this.initCount >= this.maxCount){
            return;
        } 

        let count = Math.abs(this.offsetIndex - index)
        
        if(count > 0){
            
            console.log("  index = " + index + "  count = " + count + "  this.offsetIndex = " + this.offsetIndex);
            this.offsetIndex = index;
            let tempNodeCount = this.nodeContent.childrenCount
    
            for (let i = count; i > 0; i--) {

                this.nodeContent.children.sort((a, b) => {
                    return b.position.y - a.position.y;
                });

                if(isUp){ //向上移动中
                    this.curIndex = index - i;
                    let tempNode1:cc.Node = this.nodeContent.children[0]
                    let tempNode2:cc.Node = this.nodeContent.children[tempNodeCount - 1];
                    tempNode1.position =new cc.Vec2(0,tempNode2.position.y - 88);
                    this.refreshItem(this.curIndex + this.initCount + 1 ,0,tempNode1);
                
                }else{
            
                    this.curIndex = index + i;
                    let tempNode1:cc.Node = this.nodeContent.children[0]
                    let tempNode2:cc.Node = this.nodeContent.children[tempNodeCount - 1];
                    tempNode2.position =new cc.Vec2(0,tempNode1.position.y + 88);
                    this.refreshItem(this.curIndex - 1,0,tempNode2);
                }
            }
            
        }
    }

    refreshItem(idx:number, objIdx:number, obj:cc.Node){
        if(this.refreshItemCallBack != null){
            this.refreshItemCallBack(idx,objIdx,obj);
        }
        /*
        let item:RankingItem = obj.getComponent<RankingItem>(RankingItem);
        item.textUserName.string = idx.toString()
*/
    }
}
